package mi.payment.ui.base;

import android.content.Context;
import android.os.RemoteException;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import mi.payment.R;
import mi.payment.utils.print.DataPrinterKernel;
import woyou.aidlservice.jiuiv5.IWoyouService;

/**
 * @author longx on 2016/11/19.
 */
public class StateView extends RelativeLayout {

    private ImageView mImgStateIcon;
    private TextView mTextStateTxt;
    private TextView mTextStatePrompt;

    private int printStatus = -1;

    public StateView(Context context) {
        super(context);
        initView();
    }

    public StateView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public StateView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        View mView = View.inflate(getContext(), R.layout.view_state, this);
        mImgStateIcon = (ImageView) mView.findViewById(R.id.img_state_icon);
        mTextStateTxt = (TextView) mView.findViewById(R.id.text_state_txt);
        mTextStatePrompt = (TextView) mView.findViewById(R.id.text_state_prompt);
        IWoyouService printService = DataPrinterKernel.INSTANCE().getPrinterService();
        try {
            printStatus = printService.updatePrinterState();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void setStateType(int time, StateType stateType) {
        String error;
        if (printStatus == 4) {
            error = getResources().getString(R.string.print_paper_error) + "（" + time + "s）";
        } else if (printStatus == 5) {
            error = getResources().getString(R.string.printer_hot) + "（" + time + "s）";
        } else {
            error = getResources().getString(R.string.printer_error) + "（" + time + "s）";
        }
        switch (stateType) {
            case PRINT:
                setData(R.string.print_ing, R.drawable.state_print);
                mTextStatePrompt.setText("");
                break;
            case PRINT_SUCCESS:
                setData(R.string.index_print_success, R.drawable.state_print_success);
                setPromptText(R.string.keeping_credentials);
                break;
            case PRINT_FAIL:
                setData(R.string.print_fail, R.drawable.state_print_failed);
                mTextStatePrompt.setText(error);
                break;
            case READ_CARD_FAIL_TRY:
                setData(R.string.read_card_fail, R.drawable.state_read_card_fail);
                setPromptText(R.string.try_consumption);
                break;
            case READ_CARD_FAIL_IC:
                setData(R.string.try_insert_card, R.drawable.state_read_card_fail);
                setPromptText(R.string.try_insert_card);
                break;
            case NO_ORDER:
                setData(R.string.no_order, R.drawable.state_no_order);
                break;
            case DEAL_FAIL:
                setData(R.string.deal_fail, R.drawable.state_read_card_fail);
                setPromptText(R.string.card_excess);
                break;
        }
    }

    private void setPromptText(int promptResId) {
        String text = getResources().getString(promptResId);
        mTextStatePrompt.setText(text);
    }

    private void setData(int textResId, int imgResId) {
        mImgStateIcon.setBackgroundResource(imgResId);
        String text = getResources().getString(textResId);
        mTextStateTxt.setText(text);
    }

    public enum StateType {
        PRINT, PRINT_SUCCESS, PRINT_FAIL, READ_CARD_FAIL_TRY, READ_CARD_FAIL_IC, NO_ORDER, DEAL_FAIL
    }

}
